package com.cjt.chat.controller;

import com.cjt.chat.pojo.Message;
import com.cjt.chat.pojo.User;
import com.cjt.chat.service.MessageService;
import com.cjt.chat.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cjt
 * @since 2024/3/15
 */
@CrossOrigin(origins = "http://localhost:8080")
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;

    //用户登录请求
    @PostMapping(path = "/login")
    public String login(String username, String password, HttpSession session, Model model) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(username);
        User queryOneUser = userService.queryOneUser(user);
        if (queryOneUser != null) {
            session.setAttribute("user", queryOneUser);
            return "redirect:index";
        }
        model.addAttribute("message", "用户名或密码错误!");
        return "user_login";
    }

    @GetMapping(path = "/register")
    public String register() {
        return "register";
    }

    @PostMapping(path = "/executeRegister")
    public String executeRegister(String username, String petName, String password, Model model) {
        Boolean isSuccess = userService.registerUser(username, petName, password);
        if (isSuccess) {
            model.addAttribute("success", "用户注册成功");
            return "user_login";
        }
        model.addAttribute("message", "该用户名已存在");
        return "register";
    }

    @GetMapping(path = "/index")
    public String index(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<User> friendList = userService.queryFriendList(user);
            Map<String, Message> newMessage = messageService.getNewMessage(friendList, user.getUserid());
            model.addAttribute("newMessage", newMessage);
            model.addAttribute("friendList", friendList);
            return "index";
        }
        return "redirect:/login";
    }

    @GetMapping(path = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/login";
    }

    //用户重新请求好友列表
    @ResponseBody
    @GetMapping(path = "/getFriendList")
    public Map<String, Object> getFriendList(HttpSession session) {
        Map<String, Object> resultData = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<User> friendList = userService.queryFriendList(user);
            resultData.put("friendList", friendList);
            Map<String, Message> newMessage = messageService.getNewMessage(friendList, user.getUserid());
            resultData.put("newMessage", newMessage);
            return resultData;
        }
        return null;
    }

    //搜索用户
    @ResponseBody
    @GetMapping(path = "/searchUser")
    public User searchUser(String username, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return userService.searchUser(username, user.getUserid());
        }
        return null;
    }

    @ResponseBody
    @GetMapping(path = "/addFriend")
    public void addFriend(Integer friendId, Integer messageId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            userService.addFriend(friendId, user.getUserid(), messageId);
        }
    }

    @ResponseBody
    @GetMapping(path = "/modifyPetname")
    public void modifyUserPetname(HttpSession session, String petname) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            userService.modifyUserPetname(user.getUserid(), petname);
            user.setPetName(petname);
            session.setAttribute("user", user);
        }
    }

    @ResponseBody
    @GetMapping(path = "/deleteFriend")
    public void deleteFriend(Integer friendId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            userService.deleteFriend(friendId, user.getUserid());
        }
    }
}
